package jp.co.sharp.android.parents.kidsguard.activity.edit.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.activity.edit.NoteActivity;
import jp.co.sharp.android.parents.kidsguard.activity.edit.NoteEditActivity;
import jp.co.sharp.android.parents.kidsguard.dto.NoteDto;

public class NoteItemAdaptor extends ArrayAdapter<NoteDto> {

    private int resourceId;
    private Context mContext;
    private Activity mActivity;
    public final int REQUEST_CODE_EDIT = 2;
    private final int EDIT  = 1;

    public NoteItemAdaptor(@NonNull Context context, int resource) {
        super(context, resource);
        this.resourceId = resource;
    }

    public NoteItemAdaptor(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public NoteItemAdaptor(@NonNull Context context, int resource, @NonNull NoteDto[] objects) {
        super(context, resource, objects);
    }

    public NoteItemAdaptor(@NonNull Context context, int resource, int textViewResourceId, @NonNull NoteDto[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public NoteItemAdaptor(@NonNull Activity activity, int resource, @NonNull List<NoteDto> objects) {
        super(activity, resource, objects);
        this.resourceId = resource;
        this.mActivity = activity;
    }

    public NoteItemAdaptor(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<NoteDto> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        NoteDto noteDto = getItem(position);

        View view;
        NoteItemAdaptor.ViewHolder viewHolder;

        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

            viewHolder = new NoteItemAdaptor.ViewHolder();
            viewHolder.noteInfo = view.findViewById(R.id.note_info);
            viewHolder.edit = view.findViewById(R.id.edit);
            view.setTag(viewHolder);
        } else {
            view = converView;
            viewHolder = (NoteItemAdaptor.ViewHolder) view.getTag();
        }
        viewHolder.noteInfo.setText(noteDto.getText());
        viewHolder.edit.setOnClickListener(v -> {
            Intent intent = new Intent(mActivity, NoteEditActivity.class);
            intent.putExtra("type", EDIT);
            intent.putExtra("position", position);
            intent.putExtra("note", noteDto);
            mActivity.startActivityForResult(intent, REQUEST_CODE_EDIT);
        });

        return view;
    }

    class ViewHolder {
        TextView noteInfo;
        ImageView edit;
    }
}
